﻿using Aspfm.Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aspfm.Main
{
    [Serializable()]
    public enum DataSourceType
    {
        Oracle,
        MSSql,
        MySql,
        SQLite
    }
    [Serializable()]
    public class DataSourceList : System.Runtime.Serialization.SerializationBinder
    {
        public List<DataSource> dataList = null;
        public List<DataParams> DataParamsList = new List<DataParams>();
        SerializableDictionary<DataSourceType, string> _pageSql = null;
        public SerializableDictionary<DataSourceType, string> pageSql
        {
            get
            {
                if (_pageSql == null)
                {
                    _pageSql = new SerializableDictionary<DataSourceType, string>();
                    _pageSql.Add(DataSourceType.Oracle, @"select * from (select t1.*,rownum rn from (【sql】 【orderby】) t1 where rownum <= 【endindex】) where rn > 【startindex】 【orderby】");
                    _pageSql.Add(DataSourceType.MSSql, @"SELECT * FROM (SELECT t.*,ROW_NUMBER() OVER(【orderby】) AS rn FROM (【sql】) t) AS t WHERE t.rn >【startindex】 and t.rn <= 【endindex】 【orderby】");
                    _pageSql.Add(DataSourceType.SQLite, @"【sql】 【orderby】 LIMIT 【startindex】,【pagesize】");
                }
                return _pageSql;
            }
            set
            {
                _pageSql = value;
            }
        }

        public string GetPageSql(DataSourceType datatype, string sql, string orderby, decimal pageindex, decimal pagesize)
        {
            if (pageindex <= 1) pageindex = 1;
            decimal endindex = pagesize * pageindex;
            decimal startindex = pagesize * (pageindex - 1);
            if (pageSql != null && pageSql.ContainsKey(datatype))
            {
                string sqlpage = pageSql[datatype].Replace("【startindex】", startindex.ToString()).Replace("【endindex】", endindex.ToString()).Replace("【pagesize】", pagesize.ToString());
                return sqlpage.Replace("【sql】", sql).Replace("【orderby】",orderby);
            }
            return "";
        }

        public override Type BindToType(string assemblyName, string typeName)
        {
            return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
        }
    }
    [Serializable()]
    public class DataSource : System.Runtime.Serialization.SerializationBinder
    {
        public string Key = "";
        public DataSourceType datatype = DataSourceType.Oracle;

        public string IP = "";
        public string port = "";
        public string dataSourceName = "";
        public string User = "";
        public string Pwd = "";
        public bool IsChange = false;
        public string ConnStr = "";

        public override Type BindToType(string assemblyName, string typeName)
        {
            return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
        }
    }

    [Serializable()]
    public class DataParams : System.Runtime.Serialization.SerializationBinder
    {
        public string Key = "";
        public bool IsSecret = false;
        public List<DataParamsInfo> paramslist = new List<DataParamsInfo>();

        public override Type BindToType(string assemblyName, string typeName)
        {
            return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
        }
    }
    [Serializable()]
    public class DataParamsInfo : System.Runtime.Serialization.SerializationBinder
    {
        public string Key = "";
        public string KeyValue = "";
        public string KeyRemark = "";

        public override Type BindToType(string assemblyName, string typeName)
        {
            return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
        }
    }
}
